package com.osce.score.controller;

import com.csce.domain.entity.score.ScoreExExamTime;
import com.doudou.core.str.StrUtil;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.interfaces.score.IScoreExExamService;
import com.osce.interfaces.score.IScoreExExamTimeService;
import com.osce.score.service.socket.ExPlaceSocketService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 考试webSocket连接controller
 *
 * @author zzx
 * @since 2023-09-05
 */
@RestController
@RequestMapping("/score/common")
public class ScoreController extends BaseController {

    @Resource
    private ExPlaceSocketService socketService;
    @Resource
    private IScoreExExamService examService;
    @Resource
    private IScoreExExamTimeService timeService;

    /**
     * 获取今天的考试列表
     */
    @GetMapping("/getTodayExamList")
    public AjaxResult getTodayExamList(HttpServletRequest request) {
        socketService.checkPermission(request);
        return success(examService.selectScoreExExamTodayList());
    }

    /**
     * 根据考试id获取时间分段
     */
    @GetMapping("/getExamTimeListByExamId/{examId}")
    public AjaxResult getExamTimeListById(HttpServletRequest request, @PathVariable("examId") Long examId) {
        socketService.checkPermission(request);
        List<ScoreExExamTime> scoreExExamTimes = timeService.selectScoreExExamTimeList(ScoreExExamTime.builder().examId(examId).build());
        return success(scoreExExamTimes);
    }

    /**
     * 签到
     *
     * @param body{ exmaId: 考试编号
     *              faceFeature: 人脸特征
     *              }
     */
    @PostMapping("/exam/sign")
    public AjaxResult examSignIn(HttpServletRequest request, @RequestBody Map<String, String> body) {
        socketService.checkPermission(request);
        String examId = body.get("examId");
        String timeId = body.get("timeId");
        String faceFeature = body.get("faceFeature");
        if (StrUtil.hasBlank(examId, timeId, faceFeature)) {
            return error("缺少参数");
        }
        return socketService.examSignFace(examId, timeId, faceFeature);
    }

    /**
     * 签到-备用方案，老师输入密码和学员学号
     */
    @PostMapping("/exam/sign-pwd")
    public AjaxResult examSignInByPwd(HttpServletRequest request, @RequestBody Map<String, String> body) {
        socketService.checkPermission(request);
        String examId = body.get("examId");
        String timeId = body.get("timeId");
        String teacherPwd = body.get("password");
        String stuId = body.get("stuId");
        if (StrUtil.hasBlank(examId, timeId, teacherPwd, stuId)) {
            return error("缺少参数");
        }
        return socketService.examSignPwd(examId, timeId, teacherPwd, stuId);
    }
}